package com.qf.agro.controller;

import com.qf.agro.common.ResultInfo;
import com.qf.agro.pojo.SysRole;
import com.qf.agro.service.SysRoleService;
import com.sun.xml.internal.fastinfoset.vocab.Vocabulary;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author 10202
 * @version V1.0
 * @Project smart-agriculture-parent
 * @Package com.qf.agro.controller
 * @Date 2023/5/23 11:47
 */
@RestController
@RequestMapping("/sys/")
public class SysRoleController {
    @Autowired
    private SysRoleService sysRoleService;

    @GetMapping("role/list")
    public ResultInfo getList(){
        return sysRoleService.getRoleList();
    }
    @GetMapping("role/userRole")
    public ResultInfo selectRoleIdByUserId(@RequestParam("userId") Long userId){

        return sysRoleService.selectRoleIdByUserId(userId);
    }

    @GetMapping("/role/page")
    public ResultInfo getPage(@RequestParam(value = "page", defaultValue = "1") Long page,
                              @RequestParam(value = "pageSize", defaultValue = "5") Long pageSize, @RequestParam(value = "name",defaultValue = "") String name){


        return sysRoleService.getPage(page,pageSize,name);
    }

    @PostMapping("/role/userRole")
    public ResultInfo updateRole(@RequestBody Map<String,Object> UserAndRoleMap){
      String userId = (String) UserAndRoleMap.get("userId");
      List<String> roleIds= (List<String>) UserAndRoleMap.get("roleIds");
        return sysRoleService.updateRole(userId,roleIds);
    }

    @PostMapping("role")
    public ResultInfo insertRole(@RequestBody SysRole sysRole){

        return sysRoleService.insertRole(sysRole);
    }

    @PutMapping("role")
    public ResultInfo updateRole(@RequestBody SysRole sysRole){


        return sysRoleService.updateRoleMessage(sysRole);
    }

    @DeleteMapping("role")
    public ResultInfo deleteRole(String id){
        return sysRoleService.deleteRoleById(id);
    }



}
